package cn.iocoder.yudao.module.testequipmentuse.service.testequipmentuse;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.testequipmentuse.controller.admin.testequipmentuse.vo.*;
import cn.iocoder.yudao.module.testequipmentuse.dal.dataobject.testequipmentuse.TestEquipmentUseDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.testequipmentuse.dal.mysql.testequipmentuse.TestEquipmentUseMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.testequipmentuse.enums.ErrorCodeConstants.*;

/**
 * 参试设备使用记录表 Service 实现类
 *
 * @author wwwhk
 */
@Service
@Validated
public class TestEquipmentUseServiceImpl implements TestEquipmentUseService {

    @Resource
    private TestEquipmentUseMapper testEquipmentUseMapper;

    @Override
    public Long createTestEquipmentUse(TestEquipmentUseSaveReqVO createReqVO) {
        // 插入
        TestEquipmentUseDO testEquipmentUse = BeanUtils.toBean(createReqVO, TestEquipmentUseDO.class);
        testEquipmentUseMapper.insert(testEquipmentUse);

        // 返回
        return testEquipmentUse.getId();
    }

    @Override
    public void updateTestEquipmentUse(TestEquipmentUseSaveReqVO updateReqVO) {
        // 校验存在
        validateTestEquipmentUseExists(updateReqVO.getEqId());
        // 更新
        TestEquipmentUseDO updateObj = BeanUtils.toBean(updateReqVO, TestEquipmentUseDO.class);
        testEquipmentUseMapper.updateById(updateObj);
    }

    @Override
    public void deleteTestEquipmentUse(Long id) {
        // 校验存在
        validateTestEquipmentUseExists(id);
        // 删除
        testEquipmentUseMapper.deleteById(id);
    }

    @Override
        public void deleteTestEquipmentUseListByIds(List<Long> ids) {
        // 删除
        testEquipmentUseMapper.deleteByIds(ids);
        }


    private void validateTestEquipmentUseExists(Long id) {
        if (testEquipmentUseMapper.selectById(id) == null) {
            throw exception(TEST_EQUIPMENT_USE_NOT_EXISTS);
        }
    }

    @Override
    public TestEquipmentUseDO getTestEquipmentUse(Long id) {
        return testEquipmentUseMapper.selectById(id);
    }

    @Override
    public PageResult<TestEquipmentUseDO> getTestEquipmentUsePage(TestEquipmentUsePageReqVO pageReqVO) {
        return testEquipmentUseMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<TestEquipmentUseRespVO> getTestEquipmentUsePageWithEquipmentInfo(TestEquipmentUsePageReqVO pageReqVO) {
        IPage<TestEquipmentUseDO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        IPage<TestEquipmentUseDO> resultPage = testEquipmentUseMapper.selectPageWithEquipmentInfo(page, pageReqVO);
        // 手动转换列表
        List<TestEquipmentUseRespVO> voList = CollectionUtils.convertList(
                resultPage.getRecords(),
                item -> BeanUtils.toBean(item, TestEquipmentUseRespVO.class)
        );
        return new PageResult<>(voList, resultPage.getTotal());
    }

    @Override
    public PageResult<TestEquipmentUseWithEquipmentRespVO> getTestEquipmentUseOutPageWithEquipmentInfo(TestEquipmentUsePageReqVO query) {
        // 构建分页条件
        IPage<TestEquipmentUseWithEquipmentRespVO> page = new Page<>(query.getPageNo(), query.getPageSize());
        
        // 调用分页 Mapper 方法
        IPage<TestEquipmentUseWithEquipmentRespVO> resultPage = testEquipmentUseMapper.selectPageWithEquipmentInfoByStatus(page, query, 1);
        
        // 转换为前端需要的分页结果
        return new PageResult<>(resultPage.getRecords(), resultPage.getTotal());
    }
    
    @Override
    public PageResult<TestEquipmentUseWithEquipmentRespVO> getMyEquipmentUseOutPage(TestEquipmentUsePageReqVO query) {
        // 1. 获取当前登录用户 ID（从上下文获取，确保只查当前用户的记录）
        // Long userId = SecurityFrameworkUtils.getLoginUserId();
        // query.setUserId(userId);
        
        // 2. 构建分页条件（pageNo 页码，pageSize 每页条数）
        IPage<TestEquipmentUseWithEquipmentRespVO> page = new Page<>(query.getPageNo(), query.getPageSize());
        
        // 3. 调用分页 Mapper 方法（不再用 selectOne）
        IPage<TestEquipmentUseWithEquipmentRespVO> resultPage = testEquipmentUseMapper.selectPageWithEquipmentInfoByStatus(page, query, 1);
        
        // 4. 转换为前端需要的分页结果
        return new PageResult<>(resultPage.getRecords(), resultPage.getTotal());
    }

}